<template>
    <el-dialog
        :visible.sync="is_show"
        :title="title"
        width="450px"
        @closed="closed"
        center
    >
        <el-form
            :model="form"
            ref="form"
        >
            <el-form-item
                prop="review"
                label="回复内容"
                :rules="{required: true, message: '请输入回复内容', trigger: 'blur'}"
            >
                <el-input
                    type="textarea"
                    v-model="form.review"
                    :show-word-limit="true"
                    :maxlength="500"
                    :rows="4"
                ></el-input>
            </el-form-item>
        </el-form>
        <template slot="footer">
            <el-button @click="is_show = false">取消</el-button>
            <el-button @click="submit" type="primary" v-no-more-click:3000>确认</el-button>
        </template>
    </el-dialog>
</template>

<script>
export default {
    data() {
        return {
            title: '代理人回复',
            is_show: false,
            type: null,
            form: {
                review: ''
            },
            order_id: 0,
            opinion_id: 0
        }
    },
    methods: {
        closed() {
            Object.assign(this.$data, this.$options.data())
            this.$refs['form'].resetFields()
        },
        showOn(data, type) {
            this.order_id = data.order_id
            this.opinion_id = data.id
            if (type) {
                this.type = type
                if (this.type == 'auditor') {
                    this.title = '审核员回复'
                }
            }
            this.is_show = true
        },
        submit() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    let params = {
                        opinion_id: this.opinion_id,
                        review: this.form.review
                    }
                    if (this.type) {
                        params.type = this.type
                    }
                    this.$axios.post(`/api/order/${this.order_id}/agent_message`, params).then(res => {
                        if (res.data.code == 200) {
                            this.$message({
                                type: 'success',
                                message: '回复成功！'
                            })
                            this.is_show = false
                            this.$emit('refresh')
                        } else {
                            this.$message({
                                type: 'error',
                                message: res.data.message
                            })
                        }
                    }).catch(e => {
                        console.log(e)
                    })
                }
            })
        }
    }
}
</script>

<style lang="less" scoped>
/deep/.el-dialog {
    .el-dialog__body {
        padding-bottom: 0;
        padding-top: 0;
    }
}
</style>
